﻿using AutoEditUI.UserControls;
using AutoEditUI.View.UserControls;
using MK;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace AutoEditUI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public UIModel gUIModel = new UIModel();
        public ObservableCollection<FrameworkElement> ObservableObj;
        public ObservableCollection<string> ObservableFiveCode = new ObservableCollection<string>();
        static public List<string> xyRelate = new List<string>();
        static public List<string> xyRelateNot = new List<string>();
        static public List<UIItem> gItems = new List<UIItem>();

        public MainWindow()
        {
            InitializeComponent();
            // FullScreen();
            gNowPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName.Replace(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName, "");
            LogHelper.SetListBoxLog(LogBox);
            APPConfigure.RefreshConfigure(this);
           
            LogHelper.Log("Init");
            Logic.Work(this);
            AEWork.Init(this);
            MESWork.Init(this);
            TextBoxWidth.Text = (WorkCanvas.Width / 3.7808).ToString();
            TextBoxHeight.Text = (WorkCanvas.Height / 3.7808).ToString();
            ObservableObj = new ObservableCollection<FrameworkElement>();
            lv.DataContext = ObservableObj;
            ComboBox5code.DataContext = ObservableFiveCode;
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (string.IsNullOrEmpty(TextBoxName.Text)) {
                MessageBox.Show("No Item Name");
                return;
            }
            FrameworkElement rect1 = sender as FrameworkElement;
            string ttt = rect1.Name;
            Rectangle temp = new Rectangle();
            if (ttt == "RectangleText")
            {
                temp.Tag = "Text";
            }
            else if (ttt == "RectangleIcon")
            {
                temp.Tag = "Icon";
            }
            else if (ttt == "RectangleCanvas")
            {
                temp.Tag = "Canvas";
            }
            else if (ttt == "RectangleLine")
            {
                temp.Tag = "Line";
            }
            else if (ttt == "RectangleQR")
            {
                temp.Tag = "QR";
            }
            else if (ttt == "RectangleBarCode")
            {
                temp.Tag = "BarCode";
            }
            else if (ttt == "HStackPanel")
            {
                temp.Tag = "HStackPanel";
            }
            else if (ttt == "VStackPanel")
            {
                temp.Tag = "VStackPanel";
            }
            //创建新矩形
            temp.Width = rect1.Width;
            temp.Height = rect1.Height;
            //添加移动事件
            temp.MouseLeftButtonDown += new MouseButtonEventHandler(module_MouseLeftButtonDown);
            temp.MouseMove += new MouseEventHandler(module_MouseMove);
            temp.MouseLeftButtonUp += new MouseButtonEventHandler(module_MouseLeftButtonUp);
            DragDrop.DoDragDrop(temp, temp, DragDropEffects.Copy);
        }

        internal void Work(string fullPath)
        {
            MESWork.Work(this, fullPath);
            ValueSync.UIModelSet(this, gUIModel, WorkCanvas);
        }

        private void Canvas_Drop(object sender, DragEventArgs e)
        {
            Rectangle r = (Rectangle)e.Data.GetData(typeof(Rectangle));
            r.Fill = Brushes.Red;
            Point p = e.GetPosition(WorkCanvas);
            r.SetValue(Canvas.TopProperty, p.Y - r.Height / 2);
            r.SetValue(Canvas.LeftProperty, p.X - r.Width / 2);

            string xx = TextBoxName.Text;
            string mGuid = "a"+Guid.NewGuid().ToString("N");
            if (!string.IsNullOrEmpty(xx) && NoNameIn(xx)) {
                mGuid = xx;
                TextBoxName.Text = "";
            }
            
            if ((string)r.Tag == "Icon")
            {
                ItemIcon mItemIcon = new ItemIcon();
                mItemIcon.ItemName =   mGuid;
                mItemIcon.Tag = "Icon_"  + mGuid;
                mItemIcon.W = 50;
                mItemIcon.H = 50;
                mItemIcon.Y = p.Y - 50 / 2;
                mItemIcon.X = p.X - 50 / 2;
                if (gUIModel.ItemIcons == null)
                {
                    gUIModel.ItemIcons = new System.Collections.Generic.List<ItemIcon>();
                }
                gUIModel.ItemIcons.Add(mItemIcon);
                Image x = UIInit.AddIconInit(this, WorkCanvas, mItemIcon);
                MainWindow.xyRelate.Add(mItemIcon.Tag);
                WorkPropImage(x);
            }
            else if ((string)r.Tag == "Canvas")
            {
                ItemCanvas mItemCanvas = new ItemCanvas();
                mItemCanvas.ItemName =  mGuid;
                mItemCanvas.Tag = "Canvas_"  + mGuid;
                mItemCanvas.W = 50;
                mItemCanvas.H = 50;
                mItemCanvas.Y = p.Y - 50 / 2;
                mItemCanvas.X = p.X - 50 / 2;
                if (gUIModel.ItemCanvass == null)
                {
                    gUIModel.ItemCanvass = new System.Collections.Generic.List<ItemCanvas>();
                }
                gUIModel.ItemCanvass.Add(mItemCanvas);
                Canvas x = UIInit.AddCanvasInit(this, WorkCanvas, mItemCanvas);
                MainWindow.xyRelate.Add(mItemCanvas.Tag);
                WorkCanvasProp(x);
            }
            else if ((string)r.Tag == "Text")
            {
                ItemText mItemText = new ItemText();
                mItemText.ItemName =   mGuid;
                mItemText.Tag = "Text_"  + mGuid;
                mItemText.Text = "Text";
                mItemText.Background = Brushes.White.ToString();
                mItemText.W = 60;
                mItemText.H = 12;
                mItemText.FontSize = 10;
                mItemText.Y = p.Y - 60 / 2;
                mItemText.X = p.X - 12 / 2;
                mItemText.FontFamily = @"Microsoft YaHei UI";
                mItemText.FontWeight = FontWeights.Normal.ToOpenTypeWeight();
                if (gUIModel.ItemTexts == null)
                {
                    gUIModel.ItemTexts = new System.Collections.Generic.List<ItemText>();
                }
                gUIModel.ItemTexts.Add(mItemText);
                TextBlock x = UIInit.AddText(this, WorkCanvas, mItemText);
                MainWindow.xyRelate.Add(mItemText.Tag);
                WorkTextProp(x);
            }
            else if ((string)r.Tag == "Line")
            {
                ItemRect mItemRect = new ItemRect();
                mItemRect.ItemName =  mGuid;
                mItemRect.Tag = "Line_"  + mGuid;
                mItemRect.W = 100;
                mItemRect.H = 10;
                mItemRect.Y = p.Y - 100 / 2;
                mItemRect.X = p.X - 10 / 2;
                if (gUIModel.ItemRects == null)
                {
                    gUIModel.ItemRects = new System.Collections.Generic.List<ItemRect>();
                }
                gUIModel.ItemRects.Add(mItemRect);
                Rectangle x = UIInit.AddRect(this, WorkCanvas, mItemRect);
                MainWindow.xyRelate.Add(mItemRect.Tag);
                WorkLineProp(x);
            }
            else if ((string)r.Tag == "QR")
            {
                ItemQR mItemQR = new ItemQR();
                mItemQR.ItemName =  mGuid;
                mItemQR.Tag = "QR_"  + mGuid;
                mItemQR.ItemValue = "f123456dfdfdfd";
                mItemQR.W = 50;
                mItemQR.H = 50;
                mItemQR.Y = p.Y - 50 / 2;
                mItemQR.X = p.X - 50 / 2;
                if (gUIModel.ItemQRs == null)
                {
                    gUIModel.ItemQRs = new System.Collections.Generic.List<ItemQR>();
                }
                gUIModel.ItemQRs.Add(mItemQR);
                Image x = UIInit.AddQR(this, WorkCanvas, mItemQR);
                MainWindow.xyRelate.Add(mItemQR.Tag);
                WorkQRProp(x);
            }
            else if ((string)r.Tag == "BarCode")
            {
                ItemBarcode mItemBarcode = new ItemBarcode();
                mItemBarcode.ItemName =  mGuid;
                mItemBarcode.Tag = "BarCode_"  + mGuid;
                mItemBarcode.ItemValue = "f123456dfdfdfd";
                mItemBarcode.W = 50;
                mItemBarcode.H = 50;
                mItemBarcode.Y = p.Y - 50 / 2;
                mItemBarcode.X = p.X - 50 / 2;
                if (gUIModel.ItemBarcodes == null)
                {
                    gUIModel.ItemBarcodes = new System.Collections.Generic.List<ItemBarcode>();
                }
                gUIModel.ItemBarcodes.Add(mItemBarcode);
                Image x = UIInit.AddBarcode(this, WorkCanvas, mItemBarcode);
                MainWindow.xyRelate.Add(mItemBarcode.Tag);
                WorkBarCodeProp(x);
            }

            else if ((string)r.Tag == "HStackPanel")
            {
                MKStackPanel mMKStackPanel = new MKStackPanel();
                mMKStackPanel.ItemName = mGuid;
                mMKStackPanel.Tag = "HStackPanel_" + mGuid;

                mMKStackPanel.B_H = true;


                gUIModel.MKStackPanels.Add(mMKStackPanel);

                //  MainWindow.xyRelate.Add(mItemBarcode.Tag);
                WorkHStackPanelProp();
            }

            //else if ((string)r.Tag == "VStackPanel")
            //{
            //    MKStackPanel mMKStackPanel = new MKStackPanel();
            //    mItemBarcode.ItemName = mGuid;
            //    mItemBarcode.Tag = "BarCode_" + mGuid;
            //    mItemBarcode.ItemValue = "f123456dfdfdfd";
            //    mItemBarcode.W = 50;
            //    mItemBarcode.H = 50;
            //    mItemBarcode.Y = p.Y - 50 / 2;
            //    mItemBarcode.X = p.X - 50 / 2;
            //    if (gUIModel.ItemBarcodes == null)
            //    {
            //        gUIModel.ItemBarcodes = new System.Collections.Generic.List<ItemBarcode>();
            //    }
            //    gUIModel.ItemBarcodes.Add(mItemBarcode);
            //    Image x = UIInit.AddBarcode(this, WorkCanvas, mItemBarcode);
            //    MainWindow.xyRelate.Add(mItemBarcode.Tag);
            //    WorkBarCodeProp(x);
            //}
        }

        private bool NoNameIn(string xx)
        {
            foreach (UIItem item in MainWindow.gItems)
            {
                if (item.Tag == xx) {
                    return false;
                }
            }
            return true;
        }

        private void WorkBarCodeProp(Image x)
        {
            BarCodeProp p = (BarCodeProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/BarCodeProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(p);
            p.Bind(this, x);
        }
        private void WorkHStackPanelProp()
        {
            StackPanelProp mStackPanelProp = (StackPanelProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/StackPanelProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(mStackPanelProp);
            mStackPanelProp.setHV(Orientation.Horizontal);
           // p.Bind(this, x);
        }

        private void WorkQRProp(Image x)
        {
            QRProp p = (QRProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/QRProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(p);
            p.Bind(this, x);
        }

        private void WorkLineProp(Rectangle x)
        {
            LineProp p = (LineProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/LineProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(p);
            p.Bind(this, x);
        }


        private void WorkCanvasProp(Canvas x)
        {
            CanvasProp p = (CanvasProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/CanvasProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(p);
            p.Bind(this, x);
        }
        private void WorkTextProp(TextBlock x)
        {
            TextProp p = (TextProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/TextProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(p);
            p.Bind(this, x);
        }
        private void WorkPropImage(Image x)
        {
            ImageProp p = (ImageProp)System.Windows.Application.LoadComponent(new Uri(@"UserControls/ImageProp.xaml", System.UriKind.RelativeOrAbsolute));
            SPProp.Children.Clear();
            SPProp.Children.Add(p);
            p.Bind(this, x);
        }
        Point oldPoint = new Point();
       public UIElement SelectedUIElement;
        bool isMove = false;
        private void canvas2_MouseMove(object sender, MouseEventArgs e)
        {
            UIElement msender = sender as UIElement;
            if (msender != SelectedUIElement) return;
            if (isMove)
            {
                FrameworkElement currEle = sender as FrameworkElement;
                double xPos = e.GetPosition(null).X - oldPoint.X + (double)currEle.GetValue(Canvas.LeftProperty);
                double yPos = e.GetPosition(null).Y - oldPoint.Y + (double)currEle.GetValue(Canvas.TopProperty);
                currEle.SetValue(Canvas.LeftProperty, xPos);
                currEle.SetValue(Canvas.TopProperty, yPos);
                oldPoint = e.GetPosition(null);
            }
        }
        public void canvas2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (BUIEdit.IsChecked == false) return;

            FrameworkElement currEle = sender as FrameworkElement;
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                ObservableObj.Add(sender as FrameworkElement);
            }
            else
            {
                ObservableObj.Clear();
                ObservableObj.Add(sender as FrameworkElement);
            }
            SelectedUIElement = sender as UIElement;
            SelectedUIElementLabel.Content = SelectedUIElement.GetType().ToString();

            if (typeof(TextBlock).IsInstanceOfType(sender))
            {

                TextBlock x = sender as TextBlock;
                WorkTextProp(x);
                //if ((bool)BUIMove.IsChecked)
                //{
                //    x.MouseMove += new MouseEventHandler(canvas2_MouseMove);
                //    x.MouseLeftButtonUp += new MouseButtonEventHandler(canvas2_MouseLeftButtonUp);
                //}
            }
            else if (typeof(Image).IsInstanceOfType(sender))
            {
                Image x = sender as Image;
                string xxx = ((string)x.Tag).Split('_')[0].ToLower();
                if (xxx == "qr")
                {
                    WorkQRProp(x);
                }
                else if (xxx == "barcode")
                {
                    WorkBarCodeProp(x);
                }
                else if (xxx == "icon")
                {
                    WorkPropImage(x);
                }
                //if ((bool)BUIMove.IsChecked)
                //{
                //    x.MouseMove += new MouseEventHandler(canvas2_MouseMove);
                //    x.MouseLeftButtonUp += new MouseButtonEventHandler(canvas2_MouseLeftButtonUp);
                //}
            }
            else if (typeof(Rectangle).IsInstanceOfType(sender))
            {
                Rectangle x = sender as Rectangle;

                WorkLineProp(x); 
                //if ((bool)BUIMove.IsChecked)
                //{
                //    x.MouseMove += new MouseEventHandler(canvas2_MouseMove);
                //    x.MouseLeftButtonUp += new MouseButtonEventHandler(canvas2_MouseLeftButtonUp);
                //}
            }
            else if (typeof(Canvas).IsInstanceOfType(sender))
            {
                Canvas x = sender as Canvas;

                WorkCanvasProp(x);
                //if ((bool)BUIMove.IsChecked)
                //{
                //    x.MouseMove += new MouseEventHandler(canvas2_MouseMove);
                //    x.MouseLeftButtonUp += new MouseButtonEventHandler(canvas2_MouseLeftButtonUp);
                //}
            }
            isMove = true;
            oldPoint = e.GetPosition(null);
        }

        private void canvas2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isMove = false;
            {
                UIElement x = sender as UIElement;
                x.MouseMove -= new MouseEventHandler(canvas2_MouseMove);
                x.MouseLeftButtonUp -= new MouseButtonEventHandler(canvas2_MouseLeftButtonUp);
            }
        }

        void module_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = sender as Rectangle;
            mouseposition = e.GetPosition(WorkCanvas);
            mousedown = true;
            rect.CaptureMouse();
        }
        public Point mouseposition { get; private set; }
        static public string gNowPath { get; private set; }
        bool mousedown = true;
        void module_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle rect = sender as Rectangle;
            if (mousedown)
            {
                double deltav = e.GetPosition(WorkCanvas).Y - mouseposition.Y;
                double deltah = e.GetPosition(WorkCanvas).X - mouseposition.X;
                double newtop = deltav + (double)rect.GetValue(Canvas.TopProperty);
                double newleft = deltah + (double)rect.GetValue(Canvas.LeftProperty);
                rect.SetValue(Canvas.TopProperty, newtop);
                rect.SetValue(Canvas.LeftProperty, newleft);
                mouseposition = e.GetPosition(WorkCanvas);
            }
        }

        void module_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = sender as Rectangle;
            mousedown = false;
            rect.ReleaseMouseCapture();
        }

        public void Export(Uri path, Canvas surface)
        {
            if (path == null) return;
            if (surface == null) return;
            string xaml = XamlWriter.Save(surface);
            File.WriteAllText(path.LocalPath, xaml);
        }

        public void ExportModel(string path, Canvas surface)
        {
            if (path == null) return;
            if (surface == null) return;
            XmlSerializer.SaveToXml(path, gUIModel, null, null);
        }

        private void MainWindows_Keydown(object sender, KeyEventArgs e)
        {
            if (SelectedUIElement == null) return;
            //判断用户的按键是否为Alt+F4
            if (e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt)
            {
                e.Handled = true;
                MessageBox.Show("请输入解锁密码！");
            }
            //else if (e.KeyStates == Keyboard.GetKeyStates(Key.Left) || e.KeyStates == Keyboard.GetKeyStates(Key.A))
            //{
            //    SelectedUIElement.SetValue(Canvas.LeftProperty, (double)SelectedUIElement.GetValue(Canvas.LeftProperty) - 1);
            //}
            //else if (e.KeyStates == Keyboard.GetKeyStates(Key.Right) || e.KeyStates == Keyboard.GetKeyStates(Key.D))
            //{
            //    SelectedUIElement.SetValue(Canvas.LeftProperty, (double)SelectedUIElement.GetValue(Canvas.LeftProperty) + 1);
            //}
            //else if (e.KeyStates == Keyboard.GetKeyStates(Key.Up) || e.KeyStates == Keyboard.GetKeyStates(Key.W))
            //{
            //    SelectedUIElement.SetValue(Canvas.TopProperty, (double)SelectedUIElement.GetValue(Canvas.TopProperty) - 1);
            //}
            //else if (e.KeyStates == Keyboard.GetKeyStates(Key.Down) || e.KeyStates == Keyboard.GetKeyStates(Key.S))
            //{
            //    SelectedUIElement.SetValue(Canvas.TopProperty, (double)SelectedUIElement.GetValue(Canvas.TopProperty) + 1);
            //}
            else if (e.KeyStates == Keyboard.GetKeyStates(Key.Delete))
            {
                WorkCanvas.Children.Remove(SelectedUIElement);

                Utils.RemoveItembyTag(gUIModel, (string)((FrameworkElement)SelectedUIElement).Tag);
                SelectedUIElement = null;
            }
            else if (e.KeyStates == Keyboard.GetKeyStates(Key.F5))
            {
                if (ObservableObj.Count == 2)
                {
                    GeItemByTag((string)ObservableObj[0].Tag).UpItemTag = (string)ObservableObj[1].Tag;
                }
            }
            else if (e.KeyStates == Keyboard.GetKeyStates(Key.F6))
            {
                if (ObservableObj.Count == 2)
                {
                    GeItemByTag((string)ObservableObj[0].Tag).LeftItemTag = (string)ObservableObj[1].Tag;
                }
            }

            // 相对位置的可视化编辑功能： 
            //1）按住Ctrl 同时选取两个 Item（Item1 Item2） 的时候， 按f5 按键，则对应了操作：  Item1 的top 设置为 Item2
            //2）按住Ctrl 同时选取两个 Item（Item1 Item2） 的时候， 按f6 按键，则对应了操作：  Item1 的left 设置为 Item2
        }

        private void Buttonset_Click(object sender, RoutedEventArgs e)
        {

            WorkCanvas.Width = Convert.ToDouble(TextBoxWidth.Text) *Utils.MBXUtilsType;
            WorkCanvas.Height = Convert.ToDouble(TextBoxHeight.Text) * Utils.MBXUtilsType;
            WorkCanvas.Background = Brushes.White;
        }

        private void Xduiqi_Click(object sender, RoutedEventArgs e)
        {
            if (ObservableObj.Count < 2) return;
            foreach (FrameworkElement item in ObservableObj)
            {
                Canvas.SetLeft(item, Canvas.GetLeft(ObservableObj[0]));
            }
        }

        private void Yduiqi_Click(object sender, RoutedEventArgs e)
        {
            if (ObservableObj.Count < 2) return;
            foreach (FrameworkElement item in ObservableObj)
            {
                Canvas.SetTop(item, Canvas.GetTop(ObservableObj[0]));
            }
        }

        private void ButtonLoadModel_Click_1(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "xml Files (*.xml)|*.xml"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                this.TBName.Text = openFileDialog.FileName;

                WorkCanvas.Children.Clear();
                WorkCanvas.Background = Brushes.White;
                gUIModel = (UIModel)XmlSerializer.LoadFromXml(openFileDialog.FileName, typeof(UIModel));
                ValueSync.UIModelSet(this, gUIModel, WorkCanvas);

            }
        }

        public FrameworkElement GetFrameworkElementByTag(string tag)
        {
            foreach (FrameworkElement item in WorkCanvas.Children)
            {
                if ((string)item.Tag == tag)
                {
                    return item;
                }
            }
            return null;
        }

        public UIItem GeItemByTag(string mTag)
        {
            if (gUIModel.ItemIcons != null)
                foreach (ItemIcon itemIcon in gUIModel.ItemIcons)
                {
                    if (itemIcon.Tag == (string)mTag)
                    {
                        return itemIcon;
                    }
                }
            if (gUIModel.ItemBarcodes != null)
                foreach (ItemBarcode itemBarcode in gUIModel.ItemBarcodes)
                {
                    if (itemBarcode.Tag == (string)mTag)
                    {
                        return itemBarcode;
                    }
                }
            if (gUIModel.ItemQRs != null)
                foreach (ItemQR itemQR in gUIModel.ItemQRs)
                {
                    if (itemQR.Tag == (string)mTag)
                    {
                        return itemQR;
                    }
                }
            if (gUIModel.ItemRects != null)
                foreach (ItemRect itemRect in gUIModel.ItemRects)
                {
                    if (itemRect.Tag == (string)mTag)
                    {
                        return itemRect;
                    }
                }
            if (gUIModel.ItemTexts != null)
                foreach (ItemText itemText in gUIModel.ItemTexts)
                {
                    if (itemText.Tag == (string)mTag)
                    {
                        return itemText;
                    }
                }
            if (gUIModel.ItemCanvass != null)
                foreach (ItemCanvas itemCanvas in gUIModel.ItemCanvass)
                {
                    if (itemCanvas.Tag == (string)mTag)
                    {
                        return itemCanvas;
                    }
                }
            return null;
        }

        private void ButtonSaveModel_Click(object sender, RoutedEventArgs e)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                if (!string.IsNullOrEmpty(TBName.Text))
                    Export(new Uri(TBName.Text), WorkCanvas);

            }
            else
            {

                if (!string.IsNullOrEmpty(ComboBox5code.Text))
                {
                    gUIModel = ValueSync.UIModelGet(this, WorkCanvas, gUIModel);
                    ExportModel(AppDomain.CurrentDomain.BaseDirectory + @"\" + ComboBox5code.Text/* + @"_" + MK.MKTime.Nows()*/ + @".xml", WorkCanvas);
                }
                else
                {
                    MessageBox.Show("Project NULL");
                }
            }
        }

        private void Conf_Click(object sender, RoutedEventArgs e)
        {
            Conf mConf = new Conf();
            mConf.Show();
        }

        //private void Export_Click(object sender, RoutedEventArgs e)
        //{

        //}

        private void Refesh_Click(object sender, RoutedEventArgs e)
        {
            WorkCanvas.Children.Clear();
            gUIModel = ValueSync.UIModelGet(this, WorkCanvas, gUIModel);
            ValueSync.UIModelSet(this, gUIModel, WorkCanvas);
        }

        private void Print_Click(object sender, RoutedEventArgs e)
        {
            string d1Path = MainWindow.gNowPath + @"pic";
            if (!Directory.Exists(d1Path))
            {

                Directory.CreateDirectory(d1Path);
            }
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                string bmppath = Printer.SaveAndPrint(WorkCanvas, Guid.NewGuid().ToString(), "dfdfdf", 8, d1Path, false, true, gUIModel.W* Utils.MBXUtilsType, gUIModel.H);

            }
            else if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Alt)
            {
                string bmppath = Printer.SaveAndPrint(WorkCanvas, Guid.NewGuid().ToString(), "dfdfdf", 8, d1Path, true, false, gUIModel.W* Utils.MBXUtilsType, gUIModel.H);
            }
        }

        private void MESTest_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Txt Files (*.txt)|*.TXT"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                Work(openFileDialog.FileName);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;
            this.WindowStyle = System.Windows.WindowStyle.ThreeDBorderWindow;
            // this.Topmost = true;
            //this.Left = 0.0;
            //this.Top = 0.0;
            //this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            //double xdyRelate = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            //double xdyRelate1 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            //double x = xdyRelate1 - xdyRelate;
            //double y = System.Windows.SystemParameters.PrimaryScreenHeight * x / xdyRelate1;
            //this.Height = System.Windows.SystemParameters.PrimaryScreenHeight - y;
        }

        private void WorkCanvas_KeyDown(object sender, KeyEventArgs e)
        {

        }
    }
}
